package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.List;

public class Library {

    private List<Book> libraryBooks;
    private List<Book> remainingBooks;
    private List<Movie> libraryMovies;
    private List<User> users;

    public Library() {
        this.libraryBooks = new ArrayList<Book>();
        this.remainingBooks = new ArrayList<Book>();
        this.libraryMovies = new ArrayList<Movie>();
        this.users = new ArrayList<User>();

        libraryBooks.add(new Book("Book1"));
        libraryBooks.add(new Book("Book2"));
        libraryBooks.add(new Book("Book3"));
        libraryBooks.add(new Book("Book4"));
        libraryBooks.add(new Book("Book5"));
        remainingBooks.add(new Book("Book1"));
        remainingBooks.add(new Book("Book2"));
        remainingBooks.add(new Book("Book3"));
        remainingBooks.add(new Book("Book4"));
        remainingBooks.add(new Book("Book5"));

        libraryMovies.add(new Movie("Movie1", 1991, "People1", "1"));
        libraryMovies.add(new Movie("Movie2", 1992, "People2", "2"));
        libraryMovies.add(new Movie("Movie3", 1993, "People3", "3"));
        libraryMovies.add(new Movie("Movie4", 1994, "People4", "4"));
        libraryMovies.add(new Movie("Movie5", 1995, "People5", "unrated"));

        users.add(new User("User1", "Password1", "Email1", "PhoneNo1"));
        users.add(new User("User2", "Password2", "Email2", "PhoneNo2"));
    }

    public List<Book> getBooks() {
        return remainingBooks;
    }

    public List<Book> getLibraryBooks() {
        return libraryBooks;
    }

    public List<Book> checkoutBook(Book book) {
        if (ifContainBook(this.libraryBooks, book) && ifContainBook(this.remainingBooks, book)) {
            this.remainingBooks.remove(book);
        }
        return this.remainingBooks;
    }

    public List<Book> returnBook(Book book) {
        if (ifContainBook(this.libraryBooks, book) && !ifContainBook(this.remainingBooks, book)) {
            this.remainingBooks.add(book);
        }
        return this.remainingBooks;
    }

    public boolean ifContainBook(List<Book> bookList, Book book) {
        for (Book b : bookList) {
            if (book.equals(b)) {
                return true;
            }
        }
        return false;
    }

    public List<Movie> getMovies() {
        return libraryMovies;
    }

    public List<Movie> checkoutMovie(Movie movie) {
        if (ifContainMovie(this.libraryMovies, movie)) {
            this.libraryMovies.remove(movie);
        }
        return this.libraryMovies;
    }

    private boolean ifContainMovie(List<Movie> libraryMovies, Movie movie) {
        for (Movie m : libraryMovies) {
            if (m.equals(movie)) {
                return true;
            }
        }
        return false;
    }

    public List<User> getUsers() {
        return users;
    }

    public User getOwnedUser(String name, String password) {
        User user = new User(name, password, " ", " ");
        for (User u : getUsers()) {
            if (u.equals(user)) {
                return u;
            }
        }
        return null;
    }

    public Book getReturnedBook(String bookName) {
        Book book = new Book(bookName);
        for(Book b : getLibraryBooks()){
            if(b.equals(book)){
                return b;
            }
        }
        return null;
    }
}
